////////////////////////////////////////////////////////////////////////////////
//
//  Zemoga Inc
//  Copyright 2011 Zemoga Inc
//  All Rights Reserved.
//
////////////////////////////////////////////////////////////////////////////////
package com.videojukebox.components
{
	import com.videojukebox.components.skins.VideoPlayerContainerBase;
	
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	
	import mx.events.FlexEvent;
	
	/**
	 * Class description here
	 * 
	 * @author	davidnino	<email>
	 */
	public class VideoPlayerContainer extends VideoPlayerContainerBase
	{
		//------------------------------------------------------------------------------
		//
		//  Constants
		//
		//------------------------------------------------------------------------------
		
		//--------------------------------------
		//  Public
		//--------------------------------------
		private static const DEFAULT_VIDEO_ID:String = "QzEcJecXJC4";
		private static const YOUTUBE_VIDEO_PREFIX:String ="http://www.youtube.com/v/";
		private static const YOUTUBE_VIDEO_POSTFIX:String ="?version=3";
		
		//--------------------------------------
		//  Private
		//--------------------------------------
		
		//------------------------------------------------------------------------------
		//
		//  Static Methods
		//
		//------------------------------------------------------------------------------
		
		////////////////////////////////////////////////////////////////////////////////
		//
		//  Constructor
		//
		////////////////////////////////////////////////////////////////////////////////
		
		public function VideoPlayerContainer()
		{
			addEventListener(FlexEvent.CREATION_COMPLETE, creationComplete_handler);
		}
		
		//------------------------------------------------------------------------------
		//
		//  Variables
		//
		//------------------------------------------------------------------------------
		
		//--------------------------------------
		//  Public
		//--------------------------------------
		
		//--------------------------------------
		//  Private
		//--------------------------------------
		private var player:Object;
		private var loader:Loader = new Loader();
		
		//------------------------------------------------------------------------------
		//
		//  Properties (getters/setters)
		//
		//------------------------------------------------------------------------------
		
		//------------------------------------------------------------------------------
		//
		//  Overriden methods
		//
		//------------------------------------------------------------------------------
		
		//------------------------------------------------------------------------------
		//
		//  Methods
		//
		//------------------------------------------------------------------------------
		
		//--------------------------------------
		//  Public
		//--------------------------------------
		public function loadVideo(videoID:String):void
		{
			var id:String = videoID.substring( videoID.lastIndexOf("/") + 1, videoID.length);
			loader.load(new URLRequest(YOUTUBE_VIDEO_PREFIX+ id + YOUTUBE_VIDEO_POSTFIX));
		}
		
		//--------------------------------------
		//  Private
		//--------------------------------------
		
		
		private function loadPlayer():void
		{
			loader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorEvent_handler);
			
			var resultURL:String = YOUTUBE_VIDEO_PREFIX + DEFAULT_VIDEO_ID;
			
			loader.load(new URLRequest(YOUTUBE_VIDEO_PREFIX+DEFAULT_VIDEO_ID+YOUTUBE_VIDEO_POSTFIX));
			
		}
		private function buidUI():void
		{
			loadPlayer();
		}
		
		//------------------------------------------------------------------------------
		//
		//  Event Handlers
		//
		//------------------------------------------------------------------------------
		protected function creationComplete_handler(event:FlexEvent):void
		{
			removeEventListener(FlexEvent.CREATION_COMPLETE, creationComplete_handler);
			buidUI();
		}
		
		protected function onLoaderInit(event:Event):void {
			videoHolder.addChild(loader);
			
			loader.content.addEventListener("onReady", onPlayerReady);
			loader.content.addEventListener("onError", onPlayerError);
			loader.content.addEventListener("onStateChange", onPlayerStateChange);
			loader.content.addEventListener("onPlaybackQualityChange", onVideoPlaybackQualityChange);
		}
		
		protected function ioErrorEvent_handler(event:IOErrorEvent):void
		{
			trace("IO ERROR", event.text);
		}
		
		protected function onPlayerReady(event:Event):void {
			// Event.data contains the event parameter, which is the Player API ID 
			trace("player ready:", Object(event).data);
			
			// Once this event has been dispatched by the player, we can use
			// cueVideoById, loadVideoById, cueVideoByUrl and loadVideoByUrl
			// to load a particular YouTube video.
			player = loader.content;
			// Set appropriate player dimensions for your application
			//player.setSize(480, 360);
		}
		
		protected function onPlayerError(event:Event):void {
			// Event.data contains the event parameter, which is the error code
			trace("player error:", Object(event).data);
		}
		
		protected function onPlayerStateChange(event:Event):void {
			// Event.data contains the event parameter, which is the new player state
			trace("player state:", Object(event).data);
		}
		
		protected function onVideoPlaybackQualityChange(event:Event):void {
			// Event.data contains the event parameter, which is the new video quality
			trace("video quality:", Object(event).data);
		}
	}
}